import {Request} from "../../utils/request";
import host from "../../utils/host";
import func from "../../utils/functions";
import map from "../../utils/qqmap-wx-jssdk";
const HostUrl = host.service;
const Requ = new Request();
const Func = new func();
const Map = new map({
  key: 'KE4BZ-POLWP-XAHDB-LCLHX-IML6V-E6B7R',
});
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '正在获取当前地址',
	latitude:'',
	longitude:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
    });
    
    let PromiseId = new Promise((resolve, reject)=>{
      Map.reverseGeocoder({
        location: '',
        success: (res)=>{
			console.log(res)
          this.setData({
            MS_address: res.result.address
          });
          resolve(res);
        },
        fail: (res)=>{
          reject(res);
        }
      });
    });
    PromiseId.then((res)=>{
      // 获取自提点列表
      this.data.latitude = app.globalData.latitude?app.globalData.latitude:res.result.location.lat;
      this.data.longitude = app.globalData.longitude?app.globalData.longitude:res.result.location.lng;
      this.GetSelfSite(this.data.latitude, this.data.longitude);
      // this.GetSelfSite(false, false);
    },
    (res)=>{
      Requ.showToast('获取定位有误');
    });

  },
  // 地址选择
  GetAddress: function (e) {
    wx.chooseLocation({
      success: (res) => {
        this.MS_address = res.address;
        this.MS_latitude = app.globalData.latitude?app.globalData.latitude:res.latitude;
        this.MS_longitude = app.globalData.longitude?app.globalData.longitude:res.longitude;
        this.name = res.name;
        this.setData({
          MS_address: res.name,
        });
        // 获取自提点列表
        this.GetSelfSite(this.MS_latitude, this.MS_longitude);
      },
    });
  },
  // 获取自提点列表
  GetSelfSite: function(lat, lng, self_site_id=false){
    if(lat == false || lng == false){
      var ManageAddressdata = {};
    }else{
      var ManageAddressdata = {
        latitude: lat,
        longitude: lng,
      };
      if(self_site_id){
        ManageAddressdata['self_site_id'] = app.globalData.self_site_id?app.globalData.self_site_id:self_site_id;
      }
    }
    wx.request({
      url: HostUrl.SZXURL + HostUrl.ManageAddress,
      method: 'GET',
      data: ManageAddressdata,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'openid': app.globalData.openid,
      },
      success: (setData) => {
		  console.log(setData)
        if(setData.data.data.list == undefined || setData.data.data.list.length == 0){
          Requ.showToast('附件暂无提货点');
          return;
        }
		let length = setData.data.data.list.length
		let distance = setData.data.data.list[0].juli.toFixed(2)
		let flag = 0
		for(let i=0;i<length;i++){
			setData.data.data.list[i].juli = setData.data.data.list[i].juli.toFixed(2)
			if(setData.data.data.list[i].juli<distance){
				distance=setData.data.data.list[i].juli
				flag=i
			}
		}
		let list = []
		list.push(setData.data.data.list[flag])
        this.setData({
          list: list,
        });
      }
    });
  },

  // 服务站点选择
  SelectButton: function (event) {
    let index = event.currentTarget.dataset.index;
    let self_site_id = this.data.list[index].id;  // 自提点id
    app.globalData.self_site_id = self_site_id;
    // let latitude = app.globalData.latitude?app.globalData.latitude:this.data.list[index].latitude;
    // let longitude = app.globalData.longitude?app.globalData.longitude:this.data.list[index].longitude;
    let value = {
      address: this.data.list[index]['address'],
      id: this.data.list[index]['id'],
      juli: this.data.list[index]['juli'],
      latitude: app.globalData.latitude?app.globalData.latitude:this.data.list[index]['latitude'],
      longitude: app.globalData.longitude?app.globalData.longitude:this.data.list[index]['longitude'],
      name: this.data.list[index]['name'],
      user_id: this.data.list[index]['user_id'],
    };
    app.globalData.latitude = this.data.list[index].latitude;
    app.globalData.longitude = this.data.list[index].longitude;
    Func.SaveStoragec('szx', JSON.stringify(value), (res)=>{
      console.log(res);
      Requ.showToast(`已选${this.data.list[index]['name']}`);
    });
    wx.navigateBack({
      delta: 1
    })
    return;
    this.GetSelfSite(latitude, longitude, self_site_id);
   
    Requ.showToast("该功能正在对接中...");
  },
  // 返回上一层
  goback: function (e) {
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 20)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    // console.log("已完成")
  },
})